// import { login, logout, getInfo } from '@/api/user'
// import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter,asyncRoutes,anyRoutes,constantRoutes } from '@/router'
// import router from '@/router'
// const getDefaultState = () => {
//   return {
//     token: getToken(),
//     name: '',
//     avatar: '',
//     routes:[],
//     roles:[],
//     resultAsyncRoutes:[],
//     resultAllRputes:[]
//   }
// }

// const state = getDefaultState()

// const mutations = {
//   RESET_STATE: (state) => {
//     Object.assign(state, getDefaultState())
//   },
//   SET_TOKEN: (state, token) => {
//     state.token = token
//   },
//   SET_NAME: (state, name) => {
//     state.name = name
//   },
//   SET_AVATAR: (state, avatar) => {
//     state.avatar = avatar
//   },
//   SET_RESULTASYNCROUTES(state,asyncRoutes){
//     state.resultAsyncRoutes = asyncRoutes;
//     state.resultAllRputes = constantRoutes.concat(state.resultAsyncRoutes,anyRoutes)
//     router.addRoutes(state.resultAllRputes)
//   }
// }

// const actions = {
//   // user login
//   login({ commit }, userInfo) {
//     const { username, password } = userInfo
//     return new Promise((resolve, reject) => {
//       login({ username: username.trim(), password: password }).then(response => {
//         const { data } = response
//         commit('SET_TOKEN', data.token)
//         setToken(data.token)
//         resolve()
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // get user info
//   getInfo({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       getInfo(state.token).then(response => {
//         const { data } = response

//         if (!data) {
//           return reject('Verification failed, please Login again.')
//         }

//         const { name, avatar } = data
//         // commit('SET_RESULTASYNCROUTES',computedAsyncRoutes(asyncRoutes,data.routes))
//         commit('SET_RESULTASYNCROUTES',computedAsyncRoutes(asyncRoutes,data.routes))
//         commit('SET_NAME', name)
//         commit('SET_AVATAR', avatar)
//         resolve(data)
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },



//   // user logout
//   logout({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       logout(state.token).then(() => {
//         removeToken() // must remove  token  first
//         resetRouter()
//         commit('RESET_STATE')
//         resolve()
//       }).catch(error => {
//         reject(error)
//       })
//     })
//   },

//   // remove token
//   resetToken({ commit }) {
//     return new Promise(resolve => {
//       removeToken() // must remove  token  first
//       commit('RESET_STATE')
//       resolve()
//     })
//   }
// }

// const computedAsyncRoutes = (asyncRoutes,routes) =>{
//   return  asyncRoutes.filter(item=>{
//     console.log(item)
//     if(routes.indexOf(item.name)!=-1){

//       if(item.children&&item.children.length){
//         item.children = computedAsyncRoutes(item.children,routes);
//       }

//       return true;

//     }
//   })
// }
// // const computedAsyncRoutes = (asyncRoutes,routes) =>{
// //   console.log(asyncRoutes)
// //   console.log(routes)
// // }


// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }

import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter, anyRoutes, asyncRoutes, constantRoutes } from '@/router'
import router from '@/router'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    routes: [],
    roles: [],
    //对比之后： 项目中已有的异步路由 与服务器返回的路由对比
    resultAsyncRoutes: [],
    resultAllRputes: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_RESULTASYNCROUTES: (state, asyncRoutes) => {
    state.resultAsyncRoutes = asyncRoutes;
    state.resultAllRputes = constantRoutes.concat(state.resultAsyncRoutes, anyRoutes)
    router.addRoutes(state.resultAllRputes)
  },
  SET_USERINFO: (state, userinfo) => {
    state.roles = userinfo.roles;
    state.routes = userinfo.routes;
    state.name = userinfo.name;
    state.avatar = userinfo.avatar;

  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        const { name, avatar } = data
        commit('SET_USERINFO', data);
        commit('SET_RESULTASYNCROUTES', computedAsyncRoutes(asyncRoutes, data.routes))
        // commit('SET_RESULTASYNCROUTES',1)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

const computedAsyncRoutes = (asyncRoutes, routes) => {
  // console.log(asyncRoutes)
  // console.log(routes)
  return asyncRoutes.filter(item => {
    console.log(item.path)

    for (let index = 0; index < routes.length; index++) {
      const element = routes[index];
      console.log(element)
      if (element == item.path) {
        console.log("对")
        // if (item.children) {
        //   item.children = computedAsyncRoutes(item.children, routes);
        // }
        return true;
      }
    }

    // if(routes.indexOf(item) == -1){
    //   if(item.children){
    //     item.children = computedAsyncRoutes(item.children,routes);
    //   }
    //   return true;
    // }
  })

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

